<template>
  <div>
    <div class="login">
      <div style="width: 300px" class="loginBox">
        <div class="loginBox-text">叩丁狼电商管理系统</div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              type="text"
              v-model="ruleForm.username"
              placeholder="账号"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item prop="pwd">
            <el-input
              type="password"
              v-model="ruleForm.pwd"
              placeholder="密码"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              style="width: 100%"
              type="primary"
              @click="submitForm('ruleForm')"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { loginApi } from "@/request/httpApi";
import { SETMENU } from "@/store/nutation-types";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "账号不能为空！", trigger: "blur" },
          { validator: this.valiName, trigger: "blur" },
        ],
        pwd: [
          { required: true, message: "密码不能为空！", trigger: "blur" },
          { validator: this.valipwd, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    valiName(rule, value, cd) {
      if (value.length < 3 || value.length > 20) {
        cd("账号长度3~20字符");
      } else {
        cd();
      }
    },
    valipwd(rule, value, cd) {
      if (value.length < 0 || value.length > 20) {
        cd("密码长度1~20字符");
      } else {
        cd();
      }
    },
    submitForm(from) {
      this.$refs[from].validate((vali) => {
        console.log(vali);
        console.log(this.ruleForm.username);
        if (vali) {
          // 实现登录
          loginApi({
            username: this.ruleForm.username,
            password: this.ruleForm.pwd,
          })
            .then((res) => {
              // alert("登录成功");
              if (res.errno === 0) {
                localStorage.setItem("token", res.data.token);
                this.$store.commit(SETMENU, res.data.menu);
                // 跳转
                this.$router.push("/").catch((err) => {});
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
  },
};
</script>
 
<style lang = "less" scoped>
.login {
  background-color: #324057;
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  display: block;
  .loginBox {
    margin: 300px auto;
    padding: 20px 50px;
    text-align: center;
    .loginBox-text {
      font-weight: 900;
      color: #fff;
      padding: 10px 0;
      font-size: 24px;
    }
  }
}
</style>